```vue
<template>
  <div id="app">
    <div class="login-page">
      <!-- 粒子背景 -->
      <div class="particles-bg" ref="particles"></div>

      <div class="login-card">
        <div class="login-header">
          <div class="brand">
            <div class="brand-logo">
              <i class="fas fa-user-shield"></i>
            </div>
            <div class="brand-text">
              <h1>{{ t('title') }}</h1>
              <p>{{ t('subtitle') }}</p>
            </div>
          </div>

          <div class="language-switcher">
            <span>{{ t('language') }}</span>
            <select v-model="locale" aria-label="language switch">
              <option value="zh">中文</option>
              <option value="en">English</option>
            </select>
          </div>
        </div>

        <form @submit.prevent="login" class="login-form" novalidate>
          <!-- 全局消息 -->
          <div v-if="errorMessage" class="global-error-message">
            <i class="fas fa-exclamation-circle"></i>
            {{ errorMessage }}
          </div>

          <div v-if="successMessage" class="global-success-message">
            <i class="fas fa-check-circle"></i>
            {{ successMessage }}
          </div>

          <!-- 用户名输入 -->
          <div class="form-group">
            <label for="username">{{ t('username') }}</label>
            <div class="input-wrapper">
              <input type="text" id="username" v-model="formInfo.username" maxlength="12" minlength="3"
                :placeholder="t('usernamePlaceholder')"
                :class="{ 'error': usernameError, 'success': formInfo.username && !usernameError }"
                autocomplete="username" required @input="validateUsername" @blur="validateUsername" />
              <span class="input-counter">{{ formInfo.username.length }}/12</span>
            </div>
            <div class="field-feedback">
              <span class="error-message" :class="{ 'vis-hidden': !usernameError }">
                <i class="fas fa-exclamation-circle"></i>
                {{ usernameErrorMessage }}
              </span>
            </div>
          </div>

          <!-- 密码输入 -->
          <div class="form-group">
            <label for="password">{{ t('password') }}</label>
            <div class="input-wrapper">
              <input :type="showPassword ? 'text' : 'password'" id="password" v-model="formInfo.password" maxlength="12"
                minlength="6" :placeholder="t('passwordPlaceholder')"
                :class="{ 'error': passwordError, 'success': formInfo.password && !passwordError }"
                autocomplete="current-password" required @input="validatePassword" @blur="validatePassword" />
              <button type="button" class="toggle-password" @click="togglePasswordVisibility"
                :aria-label="t('togglePassword')">
                <i :class="showPassword ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
              </button>
              <span class="input-counter">{{ formInfo.password.length }}/12</span>
            </div>
            <div class="strength-section">
              <div class="strength-meter" :class="{ 'vis-hidden': !formInfo.password }">
                <div class="strength-fill" :class="formInfo.password ? passwordStrengthClass : ''"></div>
              </div>
              <div class="strength-text" :class="{ 'vis-hidden': !formInfo.password }">
                {{ formInfo.password ? (t('passwordStrength') + ': ' + passwordStrengthText) : t('passwordStrength') }}
              </div>
            </div>

            <div class="field-feedback">
              <span class="error-message" :class="{ 'vis-hidden': !passwordError }">
                <i class="fas fa-exclamation-circle"></i>
                {{ passwordErrorMessage }}
              </span>
            </div>
          </div>

          <div class="form-row">
            <label class="remember-label">
              <input type="checkbox" v-model="rememberMe" />
              <span>{{ t('rememberMe') }}</span>
            </label>
            <a href="#" @click.prevent="goToForgotPassword" class="link">{{ t('forgotPassword') }}</a>
          </div>

          <button type="submit" class="login-btn" :disabled="!isFormValid || isLoggingIn">
            <span v-if="!isLoggingIn">
              <i class="fas fa-sign-in-alt"></i>
              {{ t('login') }}
            </span>
            <span v-else>
              <i class="fas fa-spinner fa-spin"></i>
              {{ t('loggingIn') }}
            </span>
          </button>

          <div class="register-link">
            {{ t('noAccount') }}
            <a @click.prevent="goToRegister" class="link">{{ t('registerNow') }}</a>
          </div>
        </form>
      </div>
    </div>
  </div>

</template>

<script>
import { login } from '@/api/admin/Login';
import { SetToken } from '@/utils/auth';
import { setUser } from '@/api/admin/User';
import { set } from 'lodash';
export default {
  name: 'LoginPage',
  data() {
    return {
      locale: localStorage.getItem('locale') || 'zh',
      formInfo: { username: '', password: '' },
      showPassword: false,
      rememberMe: false,
      isLoggingIn: false,
      errorMessage: '',
      successMessage: '',
      usernameError: false,
      passwordError: false,
      _particlesList: []
    };
  },
  computed: {
    messages() {
      return {
        zh: {
          title: '用户登录',
          subtitle: '欢迎回来，请登录您的账户',
          language: '语言',
          username: '账号',
          usernamePlaceholder: '请输入您的账号',
          password: '密码',
          passwordPlaceholder: '请输入您的密码',
          passwordStrength: '密码强度',
          strengthWeak: '弱',
          strengthMedium: '中',
          strengthStrong: '强',
          togglePassword: '切换密码可见性',
          rememberMe: '记住我',
          forgotPassword: '忘记密码？',
          login: '登录',
          loggingIn: '登录中...',
          noAccount: '还没有账户？',
          registerNow: '立即注册',
          usernameTooShort: '账号长度至少3个字符',
          usernameTooLong: '账号长度不能超过12个字符',
          usernameInvalid: '账号只能包含字母、数字和下划线',
          passwordTooShort: '密码长度至少6个字符',
          passwordTooLong: '密码长度不能超过12个字符',
          invalidCredentials: '账号或密码错误',
          loginSuccess: '登录成功！正在跳转...'
        },
        en: {
          title: 'Sign In',
          subtitle: 'Welcome back, please sign in to your account',
          language: 'Language',
          username: 'Username',
          usernamePlaceholder: 'Enter your username',
          password: 'Password',
          passwordPlaceholder: 'Enter your password',
          passwordStrength: 'Password Strength',
          strengthWeak: 'Weak',
          strengthMedium: 'Medium',
          strengthStrong: 'Strong',
          togglePassword: 'Toggle password visibility',
          rememberMe: 'Remember me',
          forgotPassword: 'Forgot password?',
          login: 'Sign In',
          loggingIn: 'Signing in...',
          noAccount: "Don't have an account?",
          registerNow: 'Register now',
          usernameTooShort: 'Username must be at least 3 characters',
          usernameTooLong: 'Username cannot exceed 12 characters',
          usernameInvalid: 'Only letters, numbers and underscore allowed',
          passwordTooShort: 'Password must be at least 6 characters',
          passwordTooLong: 'Password cannot exceed 12 characters',
          invalidCredentials: 'Incorrect username or password',
          loginSuccess: 'Login successful! Redirecting...'
        }
      };
    },
    t() {
      return (key) => {
        const pack = this.messages[this.locale] || this.messages.zh;
        return pack[key] || key;
      };
    },
    usernameValid() {
      const { username } = this.formInfo;
      return username.length >= 3 && username.length <= 12 && /^[a-zA-Z0-9_]+$/.test(username);
    },
    passwordValid() {
      const { password } = this.formInfo;
      return password.length >= 6 && password.length <= 12;
    },
    isFormValid() {
      return this.usernameValid && this.passwordValid;
    },
    usernameErrorMessage() {
      const { username } = this.formInfo;
      if (username.length > 0) {
        if (username.length < 3) return this.t('usernameTooShort');
        if (username.length > 12) return this.t('usernameTooLong');
        if (!/^[a-zA-Z0-9_]+$/.test(username)) return this.t('usernameInvalid');
      }
      return '';
    },
    passwordErrorMessage() {
      const { password } = this.formInfo;
      if (password.length > 0) {
        if (password.length < 6) return this.t('passwordTooShort');
        if (password.length > 12) return this.t('passwordTooLong');
      }
      return '';
    },
    passwordStrength() {
      const { password } = this.formInfo;
      if (password.length === 0) return 0;
      let strength = 0;
      if (password.length >= 8) strength += 2; else if (password.length >= 6) strength += 1;
      if (/[a-z]/.test(password)) strength += 1;
      if (/[A-Z]/.test(password)) strength += 1;
      if (/[0-9]/.test(password)) strength += 1;
      if (/[^a-zA-Z0-9]/.test(password)) strength += 1;
      return Math.min(strength, 5);
    },
    passwordStrengthClass() {
      const strength = this.passwordStrength;
      if (strength <= 2) return 'strength-weak';
      if (strength <= 4) return 'strength-medium';
      return 'strength-strong';
    },
    passwordStrengthText() {
      const strength = this.passwordStrength;
      if (strength <= 2) return this.t('strengthWeak');
      if (strength <= 4) return this.t('strengthMedium');
      return this.t('strengthStrong');
    }
  },
  watch: {
    locale(newVal) {
      localStorage.setItem('locale', newVal);
    }
  },
  methods: {
    validateUsername() {
      this.usernameError = this.formInfo.username.length > 0 && !this.usernameValid;
    },
    validatePassword() {
      this.passwordError = this.formInfo.password.length > 0 && !this.passwordValid;
    },
    togglePasswordVisibility() {
      this.showPassword = !this.showPassword;
    },
    async login() {
      if (!this.isFormValid || this.isLoggingIn) return;
      this.isLoggingIn = true;
      this.errorMessage = '';
      this.successMessage = '';
      try {
        const res = await login(this.formInfo.username, this.formInfo.password);
        console.log('res.data.data', res);
        if (res.data.code == 0) {
          throw new Error(res.data.msg);
        }
        if (!res || !res.data || !res.data.data || !res.data.data.token) {
          throw new Error('令牌不存在');
        }
        const user = JSON.stringify(res.data.data)
        setUser(user)
        SetToken(res.data.data.token);
        if (res.data.data.uid == '管理员') {
          this.$router.push('/home');
        } else if (res.data.data.uid == '普通用户') {
          this.$router.push('/UserHome');
        }
        else {
          throw new Error(this.t('invalidCredentials'));
        }
        this.successMessage = this.t('loginSuccess');
        if (this.rememberMe) {
          localStorage.setItem('rememberMe', 'true');
          localStorage.setItem('token', 'mock-jwt-token');
        } else {
          sessionStorage.setItem('token', 'mock-jwt-token');
        }

      } catch (error) {
        this.errorMessage = error.message;
      } finally {
        this.isLoggingIn = false;
      }
    },
    goToRegister() {
      this.$router.push('/register');
      console.log('跳转到注册页面');
    },
    goToForgotPassword() {
      console.log('跳转到忘记密码页面');
    },
    handleKeydown(event) {
      if (event.key === 'Enter' && this.isFormValid && !this.isLoggingIn) {
        this.login();
      }
    },
    createParticles() {
      const container = this.$refs.particles;
      if (!container) return;
      const particleCount = 15;
      for (let i = 0; i < particleCount; i++) {
        const particle = document.createElement('div');
        particle.className = 'particle';
        particle.style.left = `${Math.random() * 100}%`;
        particle.style.animationDelay = `${Math.random() * 20}s`;
        const size = 2 + Math.random() * 3;
        particle.style.width = `${size}px`;
        particle.style.height = `${size}px`;
        this._particlesList.push(particle);
        container.appendChild(particle);
      }
    },
    removeParticles() {
      const container = this.$refs.particles;
      if (!container) return;
      this._particlesList.forEach(node => {
        if (node && node.parentNode === container) {
          container.removeChild(node);
        }
      });
      this._particlesList = [];
    }
  },
  mounted() {
    if (localStorage.getItem('rememberMe') === 'true') {
      this.rememberMe = true;
    }
    document.addEventListener('keydown', this.handleKeydown);
    this.createParticles();
  },
  beforeDestroy() {
    document.removeEventListener('keydown', this.handleKeydown);
    this.removeParticles();
  }
};
</script>
<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
}

:root {
  --primary: #6366f1;
  --primary-dark: #4f46e5;
  --primary-light: #8b5cf6;
  --success: #10b981;
  --error: #ef4444;
  --warning: #f59e0b;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-300: #d1d5db;
  --gray-400: #9ca3af;
  --gray-500: #6b7280;
  --gray-600: #4b5563;
  --gray-700: #374151;
  --gray-800: #1f2937;
  --gray-900: #111827;
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
  --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
}

body {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  overflow-x: hidden;
}

.login-page {
  width: 100%;
  max-width: 420px;
  position: relative;
  z-index: 10;
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  padding: 40px 32px;
  box-shadow: var(--shadow-xl);
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.login-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
}

.login-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 32px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-logo {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--primary), var(--primary-light));
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  font-size: 20px;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

.brand-text h1 {
  font-size: 24px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--primary), var(--primary-light));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 4px;
}

.brand-text p {
  font-size: 14px;
  color: var(--gray-500);
}

.language-switcher {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--gray-600);
}

.language-switcher select {
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--gray-300);
  background: white;
  font-size: 13px;
  color: var(--gray-700);
  cursor: pointer;
  transition: all 0.2s;
}

.language-switcher select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  font-size: 14px;
  font-weight: 500;
  color: var(--gray-700);
}

.input-wrapper {
  position: relative;
}

.input-wrapper input {
  width: 100%;
  padding: 14px 16px;
  border: 1.5px solid var(--gray-300);
  border-radius: 12px;
  font-size: 15px;
  transition: all 0.3s ease;
  background: white;
  padding-right: 100px;
}

.input-wrapper input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.input-wrapper input.error {
  border-color: var(--error);
}

.input-wrapper input.success {
  border-color: var(--success);
}

.input-counter {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 12px;
  color: var(--gray-400);
}

.toggle-password {
  position: absolute;
  right: 45px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--gray-500);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: color 0.2s;
}

.toggle-password:hover {
  color: var(--primary);
}

.error-message {
  font-size: 13px;
  color: var(--error);
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
}

.field-feedback {
  min-height: 20px;
  /* 预留错误文本空间，避免跳动 */
}

.strength-section {
  min-height: 30px;
  /* 预留强度条与文字高度 */
}

.vis-hidden {
  visibility: hidden;
}

.form-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
}

.remember-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--gray-600);
  cursor: pointer;
}

.remember-label input {
  width: 16px;
  height: 16px;
  accent-color: var(--primary);
}

.link {
  color: var(--primary);
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  transition: color 0.2s;
}

.link:hover {
  color: var(--primary-dark);
  text-decoration: underline;
}

.login-btn {
  background: linear-gradient(135deg, var(--primary), var(--primary-light));
  color: white;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(99, 102, 241, 0.5);
}

.login-btn:disabled {
  background: var(--gray-400);
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.register-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: var(--gray-600);
}

.global-error-message {
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.2);
  color: var(--error);
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  animation: fadeIn 0.3s ease;
}

.global-success-message {
  background: rgba(16, 185, 129, 0.1);
  border: 1px solid rgba(16, 185, 129, 0.2);
  color: var(--success);
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }

  to {
    transform: rotate(360deg);
  }
}

.particles-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  overflow: hidden;
  pointer-events: none;
}

.particle {
  position: absolute;
  width: 4px;
  height: 4px;
  background: rgba(255, 255, 255, 0.7);
  border-radius: 50%;
  animation: float 25s infinite linear;
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
  pointer-events: none;
}

@keyframes float {
  0% {
    transform: translateY(100vh) translateX(0) rotate(0deg);
    opacity: 0;
  }

  10% {
    opacity: 1;
  }

  90% {
    opacity: 1;
  }

  100% {
    transform: translateY(-100vh) translateX(100px) rotate(360deg);
    opacity: 0;
  }
}

.strength-meter {
  margin-top: 8px;
  height: 4px;
  border-radius: 2px;
  background: var(--gray-200);
  overflow: hidden;
}

.strength-fill {
  height: 100%;
  width: 0%;
  transition: width 0.3s ease, background 0.3s ease;
  border-radius: 2px;
}

.strength-weak {
  background: var(--error);
  width: 33%;
}

.strength-medium {
  background: var(--warning);
  width: 66%;
}

.strength-strong {
  background: var(--success);
  width: 100%;
}

.strength-text {
  font-size: 12px;
  color: var(--gray-500);
  margin-top: 4px;
}

@media (max-width: 480px) {
  .login-card {
    padding: 30px 20px;
  }

  .login-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }

  .language-switcher {
    align-self: flex-end;
  }
}
</style>